home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group98a.txt
/
000018_icon-group-sender _Fri Jan 23 12:33:53 1998.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
4KB
Return-Path: <icon-group-sender>
Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
by baskerville.CS.Arizona.EDU (8.8.7/8.8.7) with SMTP id MAA12604
for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Fri, 23 Jan 1998 12:33:53 -0700 (MST)
Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
id AA02606; Fri, 23 Jan 1998 12:33:53 -0700
Date: Fri, 23 Jan 1998 10:42:53 -0700
From: swampler@noao.edu (Steve Wampler)
Subject: Re: Stripping blank and comment lines
To: icon-group@optima.CS.Arizona.EDU
Message-Id: <swampler-9800231742.AA007714605@orpheus.gemini.edu>
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Content-Length: 3070
A while ago I posed a problem to this group to write a program that
strips comment lines (lines with only a comment on them) and
blank lines from Icon source files. Only Nevin Liber provided
a solution (thanks Nevin!), which works just fine and is reproduced
below. I've also included the solution I had, which is similar
to Nevin's but less sophisticated about error checking. Finally,
I've included a version that attempts to avoid the Wrath of the
Variable Name gods - it's a rewrite of my solution without any
local variables! Now a new puzzle - can you rewrite the if test
it that version's 'stripcomments()' procedure to avoid the the
empty 'then' clause?
----First, Nevin Liber's solution (I've scrunched some of the blank
lines to save space) - note the clever way he has of keeping
the file opening and closing isolated into a single procedure, while
processing the file contents outside that procedure. I had not
seen this before!:
procedure main(LArguments)
local fInput
local sLine
every fInput := InputFiles(LArguments) do {
while sLine := read(fInput) do {
write(ValidSourceLine(sLine))
}
}
end
procedure InputFiles(LFilenames)
local sFilename
local fFile
if 0 = *LFilenames then {
return &input
}
every sFilename := !LFilenames do {
if not (fFile := open(sFilename)) then {
write(&errout, "Cannot open ", image(sFilename), " for reading.")
next
}
suspend fFile
close(fFile)
}
end
procedure ValidSourceLine(sLine)
sLine ? {
tab(many('\t '))
if ="#" | not move(1) then {
fail
}
}
return sLine
end
-------------------------------------------------
Now my solution:
-------------------------------------------------
# remove comment lines and blank lines from Icon programs...
# (one can argue that a blank line is just an exceptionally
# terse comment...)
procedure main(args)
local f
if *args = 0 then {
stripcomments(&input)
}
else {
every f := open(!args) do {
stripcomments(f)
close(f)
}
}
end
procedure stripcomments(f)
local line
every line := !f do {
line ? {
tab(many(' \t'))
if ="#" | pos(0) then next
}
write(line)
}
end
------------------------------------
And, finally, the 'variable-less' version:
------------------------------------
# remove comment lines and blank lines from Icon programs...
# (one can argue that a blank line is just an exceptionally
# terse comment...)
procedure main(args)
if *args = 0 then {
stripcomments(&input)
}
else {
every close(stripcomments(open(!args)))
}
end
procedure stripcomments(f)
every !f ? {
tab(many(' \t'))
if ="#" | pos(0) then {} # can you remove this empty clause?
else write(&subject)
}
return f
end
--
Steve Wampler - swampler@gemini.edu [Gemini 8m Telescopes Project (under AURA)]
The gods that smiled at your birth are now laughing openly. (Fortune Cookie)